import 'package:concierge/app/req/apis/CommonApi.dart';
import 'package:concierge/app/req/apis/UserApi.dart';
import 'package:concierge/app/routes/app_routes.dart';
import 'package:concierge/app/service/GlobalService.dart';
import 'package:concierge/generated/locales.g.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

import 'state.dart';

enum BindPhoneType { email, google }

class BindPhoneParam {
  final BindPhoneType type;
  final Map obj;

  BindPhoneParam({required this.type, required this.obj});
}

class BindPhoneLogic extends GetxController {
  final BindPhoneState state = BindPhoneState();
  late BindPhoneParam param;
  final globalService = Get.find<GlobalService>();
  @override
  void onInit() {
    super.onInit();
    param = Get.arguments as BindPhoneParam;
    state.phoneNumber.value = globalService.lastPhone;
    getCountryCode();
  }

  Future<void> getCountryCode() async {
    var ccs = (await CommonApi.instance.getCountryCode())?.list ?? [];
    state.ccList.clear();
    state.selectCc.value = ccs.firstWhereOrNull((m) => m.callingCode == 86);
    state.ccList.addAll(ccs);
  }

  Future<void> sendSmsCode() async {
    if (state.selectCc.value == null) {
      SmartDialog.showToast(LocaleKeys.select_area_code.tr);
      return;
    }
    if (state.phoneNumber.value.isEmpty) {
      SmartDialog.showToast(LocaleKeys.input_phone_number.tr);
      return;
    }
    var hasSend = await CommonApi.instance.sendSmsCode(
      cc: state.selectCc.value!.callingCode,
      phone_number: state.phoneNumber.value,
    );
    if (hasSend) {
      SmartDialog.showToast(LocaleKeys.code_sent.tr);
      globalService.startSmsCountDown(state.phoneNumber.value);
      state.sms.value = "";
    } else {
      SmartDialog.showToast(LocaleKeys.send_code_failed.tr);
    }
  }

  Future<void> bind() async {
    if (state.selectCc.value == null) {
      SmartDialog.showToast(LocaleKeys.select_area_code.tr);
      return;
    }
    if (state.phoneNumber.value.isEmpty) {
      SmartDialog.showToast(LocaleKeys.enter_phone_number.tr);
      return;
    }
    if (state.sms.value.isEmpty) {
      SmartDialog.showToast(LocaleKeys.verify_code.tr);
      return;
    }

    if (param.type == BindPhoneType.email) {
      var user = await UserApi.instance.loginByBindEmail(
        phone: state.phoneNumber.value,
        code_phone: state.sms.value,
        session_id: param.obj['session_id'],
        email: param.obj['email'],
        code_email: param.obj['code_email'],
        cc: state.selectCc.value!.callingCode,
      );
      if (user != null) {
        globalService.cancelPhone();
        Get.offAllNamed(Routes.main);
      }
    }

    if (param.type == BindPhoneType.google) {
      var user = await UserApi.instance.loginByBindGoogle(
        phone: state.phoneNumber.value,
        code_phone: state.sms.value,
        session_id: param.obj['session_id'],
        cc: state.selectCc.value!.callingCode,
        idToken: param.obj['idToken'],
      );
      if (user != null) {
        globalService.cancelPhone();
        Get.offAllNamed(Routes.main);
      }
    }
  }
}
